//AS3///////////////////////////////////////////////////////////////////////////
// 
// Copyright(c) 2010 180heartbeats. All rights reserved.
// Please read this carefully before using the source code.
// 
// Redistribution and use in source and binary forms, with or without 
// modification, are permitted provided that the following conditions are
// met:
// 
// * Redistributions of source code must retain the above copyright notice, 
//   this list of conditions and the following disclaimer.
// 
// * Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the 
//   documentation and/or other materials provided with the distribution.
// 
// * Neither the name of 180heartbeats nor the names of its 
//   contributors may be used to endorse or promote products derived from 
//   this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
////////////////////////////////////////////////////////////////////////////////

package {
	import com.heartbeats.translate.Translate;
	import com.heartbeats.translate.TranslateEvent;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;

	[SWF(width='800', height='600', backgroundColor='#FFFFFF', frameRate='20')]

	/**
	 * @langversion ActionScript 3.0
	 * @playerversion Flash 9.0
	 * 
	 * @author Piotr Mederak
	 */
	public class TranslateExample extends Sprite {
		private var input_tf:TextField;
		private var outputEN_tf:TextField;
		private var translatePL2EN:Translate;
		private var outputAR_tf:TextField;
		private var translatePL2AR:Translate;

		public function TranslateExample() {
			// Tworze obiekt odpowiadajacy za tlumaczenie
			this.translatePL2EN = new Translate();
			this.translatePL2EN.addEventListener(TranslateEvent.COMPLETE, onTranslateENComplete);

			this.translatePL2AR = new Translate();
			this.translatePL2AR.addEventListener(TranslateEvent.COMPLETE, onTranslateARComplete);

			// pola tekstowe potrzebne do zobrazowania przykladu			
			var format:TextFormat = new TextFormat('Verdana', 11);
			
			this.input_tf = new TextField();
			this.input_tf.defaultTextFormat = format;
			
			this.input_tf.border = true;
			this.input_tf.type = TextFieldType.INPUT;
			this.input_tf.multiline = false;			this.input_tf.wordWrap = true;
			this.input_tf.width = 400;
			this.input_tf.height = 50;
			
			this.input_tf.addEventListener(Event.CHANGE, onInputChange, false, 0, true);
			
			this.input_tf.y = 10;
			this.input_tf.x = 10;
			this.addChild(this.input_tf);
			
			// pole na tlumaczenie angielskie
			this.outputEN_tf = new TextField();
			this.outputEN_tf.defaultTextFormat = format;
			
			this.outputEN_tf.multiline = true;
			this.outputEN_tf.wordWrap = true;
			this.outputEN_tf.width = 400;
			this.outputEN_tf.height = 50;
			this.outputEN_tf.y = 60;
			this.outputEN_tf.x = 10;
			
			this.addChild(this.outputEN_tf);

			// pole na tlumaczenie arabskie
			this.outputAR_tf = new TextField();
			this.outputAR_tf.defaultTextFormat = format;
			
			this.outputAR_tf.multiline = true;
			this.outputAR_tf.wordWrap = true;
			this.outputAR_tf.width = 400;
			this.outputAR_tf.height = 50;
			this.outputAR_tf.y = 110;
			this.outputAR_tf.x = 10;
			
			this.addChild(this.outputAR_tf);


			// wstawiam domyslna, przykladowa tresc do tlumaczenia
			this.input_tf.text = 'To jest przykładowa wiadomość przetłumaczona na angielski oraz arabski.';
			this.stage.focus = this.input_tf;
			// rozpoczynam tlumaczenie
			this.submitForm();
		}

		private function onInputChange(event:Event):void {
			this.submitForm();
		}

		private function submitForm():void {
			// tlumacze na oba jezyki jednoczesnie
			this.translatePL2EN.translate(this.input_tf.text, 'pl', 'en');			this.translatePL2AR.translate(this.input_tf.text, 'pl', 'ar');
		}

		private function onTranslateENComplete(event:TranslateEvent):void {
			this.outputEN_tf.text = event.translateText;
		}

		private function onTranslateARComplete(event:TranslateEvent):void {
			this.outputAR_tf.text = event.translateText;
		}
	}
}
